package cn.apthink.schedule;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import cn.apthink.utility.Util;
import lombok.Data;

@Data
public class Soluation {
	private List<PlanKPI> planKPIs;
	private Long createtime;
	private long score;
	
	public Soluation() {
		this.createtime = System.currentTimeMillis();
		this.score = 0;
	}

	public List<String> getIds() {
		List<String> ids = new ArrayList<String>();
		for (int i=0,l=planKPIs.size();i<l;i++) {
			ids.add(planKPIs.get(i).getPlanId());
		}
		return ids;
	}
	
	public void setPlanKPIs(List<PlanKPI> planKPIs) {
		this.planKPIs = planKPIs;
		//David: 积分计算和没有最大化生产效率计划数
		score = 0;
		for (int i=0,l=planKPIs.size();i<l;i++) {
			PlanKPI pk = planKPIs.get(i);
			score += pk.getScore();
			//David: 缺省计算延误计划数
			if (pk.isDelay()) --score;
			//David: 没有效率指标时不计算未最大化生产效率计划数
			if (!Util.isEmpty(pk.getMaxEfficiency()) && !pk.getMaxEfficiency()) --score;
		}
	}
	

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Soluation other = (Soluation) obj;
		return Objects.equals(planKPIs, other.planKPIs);
	}

	@Override
	public int hashCode() {
		return Objects.hash(planKPIs);
	}

	@Override
	public String toString() {
		return "Soluation [" + getScore() + planKPIs + "]";
	}

	public String toSimple() {
		StringBuilder s = new StringBuilder();
		s.append("[");
		planKPIs.forEach(x -> {
			s.append(x.getPlanId() + "(" + x.getDepartmentId() + ")");
		});
		s.append("]");
		return "Soluation [" + getScore() + s.toString() + "]";
	}

}
